<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取一个子集</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.criteria.html">设置查询条件</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.indexes.html">建立索引</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.multi.query" class="section">
   <h2 class="title">获取一个子集</h2>
   <p class="para">
    我们可以用查询条件从集合中获得多个文档。
    例如：想要获得 <em>&quot;i&quot;</em> &gt; <em>50</em> 的文档，我们需要：
   </p>
   <div class="example" id="mongo.tutorial.multi.query-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"i"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(&nbsp;</span><span style="color: #DD0000">'$gt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">50&nbsp;</span><span style="color: #007700">)&nbsp;);&nbsp;</span><span style="color: #FF8000">//note&nbsp;the&nbsp;single&nbsp;quotes&nbsp;around&nbsp;'$gt'<br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$coll</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">);<br /><br />while&nbsp;(&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hasNext</span><span style="color: #007700">()&nbsp;)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getNext</span><span style="color: #007700">()&nbsp;);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     将会显示 <em>&quot;i&quot;</em> &gt; <em>50</em>
     的文档。也可以获得一个范围内的文档，比如
     <em>20 &lt; i &lt;= 30</em>：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?php
$connection = new MongoClient();
$collection = $connection-&gt;database-&gt;collectionName;

$query = array( &#039;i&#039; =&gt; array( &#039;$gt&#039; =&gt; 20, &quot;\$lte&quot; =&gt; 30 ) );
$cursor = $coll-&gt;find( $query );

while ( $cursor-&gt;hasNext() )
{
    var_dump( $cursor-&gt;getNext() );
}
?&gt;
</pre></div>
    </div>
   </div>
   <p class="para">
    要注意美元字符始终都需要转义，或者可以用单引号代替。否则 PHP 会把它解析成对应的变量 <var class="varname"><var class="varname">$gt</var></var>。
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.criteria.html">设置查询条件</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.indexes.html">建立索引</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
